#!/bin/bash
# https://www.jacoco.org/jacoco/
# https://www.jacoco.org/jacoco/trunk/doc/
# 运行前先切换到当前文件所在目录
action=$1
cur_path=$(pwd)
jar_path="${cur_path}/../out/production/java_hello/demo.jar"
class_file_path="${cur_path}/../out/production/java_hello/"
source_file_path="${cur_path}/../src/"
jacoco_lib_path="${cur_path}/../jacoco-lib"
jacoco_agent_path="${jacoco_lib_path}/jacocoagent.jar"
jacoco_cli_path="${jacoco_lib_path}/jacococli.jar"
port=56666

if [ "${action}" = "run" ];then
  echo "run"
  java -javaagent:"${jacoco_agent_path}"=output=tcpserver,port="${port}" -jar "${jar_path}"
fi

if [ "${action}" = "exec" ];then
  echo "exec"
  rm -rf "${cur_path}/result/" -y
  mkdir -p "${cur_path}/result/"
  java -jar "${jacoco_cli_path}" dump --address 127.0.0.1 --port "${port}" --destfile "${cur_path}/result/dump.exec" --reset
fi

if [ "${action}" = "report" ];then
  echo "report"
  rm -rf "${cur_path}/report/" -y
  mkdir -p "${cur_path}/report/"
  java -jar "${jacoco_cli_path}" report "${cur_path}/result/dump.exec"  --classfiles "${class_file_path}" --sourcefiles "${source_file_path}" --html "${cur_path}/report/html" --xml "${cur_path}/report/xml_data.xml"
fi
